#include "mydialog.h"
#include "ui_mydialog.h"
#include <QDebug>
#include <QString>
#include "dialognewuser.h"
#include <QSqlTableModel>
#include <QMessageBox>
#include <QSqlRecord>
#include <QCompleter>

MyDialog::MyDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::MyDialog)
{
    ui->setupUi(this);
    setWindowTitle(tr("登陆"));//窗口标题
    QStringList strings;
    strings << "土豆" << "西瓜" ;
    completer = new QCompleter(strings, this);
    ui->comboBox->clear();
    ui->comboBox->addItems(strings);
    ui->comboBox->setEditable(true);
    ui->comboBox->setCompleter(completer);
    qDebug()<<__FUNCTION__<<"is running !";

    ui->lineEditNumber->setEchoMode(QLineEdit::Password);//密码方式显示文本
    QDialog::setModal( true );//设置模态，让该窗口一直在最前端
    setAttribute(Qt::WA_DeleteOnClose,true);//关闭后释放内存
    ui->lineEditNumber->setFocus();//设置焦点


}

MyDialog::~MyDialog()
{
    delete ui;
}

void MyDialog::on_pushButton_clicked()
{

     qDebug()<<ui->comboBox->currentText();
     QSqlTableModel model;
     model.setTable("user");
     model.select();
     int userCnt = 0;
     for(int i=0;i<model.rowCount();++i)
     {
         QSqlRecord record = model.record(i);
         QString name = record.value("name").toString();
         QString passWord = record.value("passWord").toString();
         qDebug()<<name<<passWord<<__FUNCTION__;
         if(ui->comboBox->currentText() == name){
             qDebug()<<"用户名正确!";
             if(ui->lineEditNumber->text() == passWord){

           accept();

             }

             userCnt++;
         }
     }
     if(!userCnt)
     {
         QMessageBox msgBox(this);
         msgBox.setText("该用户不存在！");
         msgBox.exec();
     }

}

void MyDialog::on_pushButton_2_clicked()
{
    DialogNewUser *user = new DialogNewUser(this);
    user->show();
}
